//
//  KKConstraintMaker.h
//  Masonry
//
//  Created by Jonas Budelmann on 20/07/13.
//  Copyright (c) 2013 cloudling. All rights reserved.
//

#import "KKConstraint.h"
#import "KKUtilities.h"

typedef NS_OPTIONS(NSInteger, KKAttribute) {
    KKAttributeLeft = 1 << NSLayoutAttributeLeft,
    KKAttributeRight = 1 << NSLayoutAttributeRight,
    KKAttributeTop = 1 << NSLayoutAttributeTop,
    KKAttributeBottom = 1 << NSLayoutAttributeBottom,
    KKAttributeLeading = 1 << NSLayoutAttributeLeading,
    KKAttributeTrailing = 1 << NSLayoutAttributeTrailing,
    KKAttributeWidth = 1 << NSLayoutAttributeWidth,
    KKAttributeHeight = 1 << NSLayoutAttributeHeight,
    KKAttributeCenterX = 1 << NSLayoutAttributeCenterX,
    KKAttributeCenterY = 1 << NSLayoutAttributeCenterY,
    KKAttributeBaseline = 1 << NSLayoutAttributeBaseline,

    KKAttributeFirstBaseline = 1 << NSLayoutAttributeFirstBaseline,
    KKAttributeLastBaseline = 1 << NSLayoutAttributeLastBaseline,
    
#if TARGET_OS_IPHONE || TARGET_OS_TV
    
    KKAttributeLeftMargin = 1 << NSLayoutAttributeLeftMargin,
    KKAttributeRightMargin = 1 << NSLayoutAttributeRightMargin,
    KKAttributeTopMargin = 1 << NSLayoutAttributeTopMargin,
    KKAttributeBottomMargin = 1 << NSLayoutAttributeBottomMargin,
    KKAttributeLeadingMargin = 1 << NSLayoutAttributeLeadingMargin,
    KKAttributeTrailingMargin = 1 << NSLayoutAttributeTrailingMargin,
    KKAttributeCenterXWithinMargins = 1 << NSLayoutAttributeCenterXWithinMargins,
    KKAttributeCenterYWithinMargins = 1 << NSLayoutAttributeCenterYWithinMargins,

#endif
    
};

/**
 *  Provides factory methods for creating KKConstraints.
 *  Constraints are collected until they are ready to be installed
 *
 */
@interface KKConstraintMaker : NSObject

/**
 *	The following properties return a new KKViewConstraint
 *  with the first item set to the makers associated view and the appropriate KKViewAttribute
 */
@property (nonatomic, strong, readonly) KKConstraint *left;
@property (nonatomic, strong, readonly) KKConstraint *top;
@property (nonatomic, strong, readonly) KKConstraint *right;
@property (nonatomic, strong, readonly) KKConstraint *bottom;
@property (nonatomic, strong, readonly) KKConstraint *leading;
@property (nonatomic, strong, readonly) KKConstraint *trailing;
@property (nonatomic, strong, readonly) KKConstraint *width;
@property (nonatomic, strong, readonly) KKConstraint *height;
@property (nonatomic, strong, readonly) KKConstraint *centerX;
@property (nonatomic, strong, readonly) KKConstraint *centerY;
@property (nonatomic, strong, readonly) KKConstraint *baseline;

@property (nonatomic, strong, readonly) KKConstraint *firstBaseline;
@property (nonatomic, strong, readonly) KKConstraint *lastBaseline;

#if TARGET_OS_IPHONE || TARGET_OS_TV

@property (nonatomic, strong, readonly) KKConstraint *leftMargin;
@property (nonatomic, strong, readonly) KKConstraint *rightMargin;
@property (nonatomic, strong, readonly) KKConstraint *topMargin;
@property (nonatomic, strong, readonly) KKConstraint *bottomMargin;
@property (nonatomic, strong, readonly) KKConstraint *leadingMargin;
@property (nonatomic, strong, readonly) KKConstraint *trailingMargin;
@property (nonatomic, strong, readonly) KKConstraint *centerXWithinMargins;
@property (nonatomic, strong, readonly) KKConstraint *centerYWithinMargins;

#endif

/**
 *  Returns a block which creates a new KKCompositeConstraint with the first item set
 *  to the makers associated view and children corresponding to the set bits in the
 *  KKAttribute parameter. Combine multiple attributes via binary-or.
 */
@property (nonatomic, strong, readonly) KKConstraint *(^attributes)(KKAttribute attrs);

/**
 *	Creates a KKCompositeConstraint with type KKCompositeConstraintTypeEdges
 *  which generates the appropriate KKViewConstraint children (top, left, bottom, right)
 *  with the first item set to the makers associated view
 */
@property (nonatomic, strong, readonly) KKConstraint *edges;

/**
 *	Creates a KKCompositeConstraint with type KKCompositeConstraintTypeSize
 *  which generates the appropriate KKViewConstraint children (width, height)
 *  with the first item set to the makers associated view
 */
@property (nonatomic, strong, readonly) KKConstraint *size;

/**
 *	Creates a KKCompositeConstraint with type KKCompositeConstraintTypeCenter
 *  which generates the appropriate KKViewConstraint children (centerX, centerY)
 *  with the first item set to the makers associated view
 */
@property (nonatomic, strong, readonly) KKConstraint *center;

/**
 *  Whether or not to check for an existing constraint instead of adding constraint
 */
@property (nonatomic, assign) BOOL updateExisting;

/**
 *  Whether or not to remove existing constraints prior to installing
 */
@property (nonatomic, assign) BOOL removeExisting;

/**
 *	initialises the maker with a default view
 *
 *	@param	view	any KKConstraint are created with this view as the first item
 *
 *	@return	a new KKConstraintMaker
 */
- (id)initWithView:(KK_VIEW *)view;

/**
 *	Calls install method on any KKConstraints which have been created by this maker
 *
 *	@return	an array of all the installed KKConstraints
 */
- (NSArray *)install;

- (KKConstraint * (^)(dispatch_block_t))group;

@end
